"""
作者: 冯中华
页面: 订单查询
"""
from Common.Base import Base, open_browser

admin_login_url = 'http://172.16.11.202:8080/ecshop/admin/privilege.php?act=login'


class OrderQueryPage(Base):
    """封装表现层: 制作定位器过程"""
    cancel_submit_loc = ('css selector', 'div.panel-cross span')  # Ｘ按钮
    ecshop_loc = ('id', 'cloudLogin')  # ECShop定位
    username_loc = ('name', 'username')  # 用户名输入框
    password_loc = ('name', 'password')  # 密码输入框
    login_submit_loc = ('class name', 'btn-a')  # 登录按钮
    menu_frame_loc = ('css selector', 'frame#menu-frame')  # 左侧菜单栏frame
    order_submit_loc = ('css selector', 'li.icon-order')  # 订单管理
    main_frame_loc = ('css selector', 'frame#main-frame')  # 订单列表主体frame
    input_order_number_loc = ('id', 'order_sn')  # 订单号输入框
    select_order_status_loc = ('css selector', 'select#status option')  # 订单状态选择请选择
    search_submit_loc = ('class name', 'button')  # 搜索按钮
    order_number_loc = ('id', 'order_0')  # 订单号
    total_amount_loc = ('css selector', 'div#listDiv table tbody tr td:nth-of-type(4)')  # 总金额
    order_status_loc = ('css selector', 'div#listDiv table tbody tr td:nth-of-type(6)')  # 订单状态

    """封装操作层: 针对表现层中元素的操作"""

    def login(self, username, password):
        """
        登录账号
        @param username:用户名
        @param password:密码
        @return:
        """
        self.click(self.cancel_submit_loc)  # 点击Ｘ按钮
        self.click(self.ecshop_loc)  # 选择ECShop登录
        self.send_keys(self.username_loc, username)  # 输入用户名
        self.send_keys(self.password_loc, password)  # 输入密码
        self.click(self.login_submit_loc)  # 点击登录按钮

    def into_order_query(self):
        """
        进入订单管理
        @return:
        """
        self.into_frame(self.menu_frame_loc)  # 进入左侧菜单栏frame
        self.click(self.order_submit_loc)  # 进入订单管理
        self.out_frame()  # 退出frame

    def order_query(self, order_number):
        """
        查询订单
        @param order_number: 需要查询的订单号
        @return:
        """
        self.into_frame(self.main_frame_loc)  # 进入订单列表主体frame
        self.send_keys(self.input_order_number_loc, order_number)  # 输入订单号
        self.click(self.select_order_status_loc)  # 订单状态选择 请选择
        self.click(self.search_submit_loc)  # 点击搜索按钮

    def get_order_information(self):
        """
        获取订单号,总金额,订单状态
        @return:
        """
        return self.get_element_text(self.order_number_loc), self.get_element_text(
            self.total_amount_loc), self.get_element_text(self.order_status_loc)


if __name__ == '__main__':
    query = OrderQueryPage(open_browser())  # 打开浏览器
    query.open_url(admin_login_url)  # 进入登录页面
    query.login('root', 'root123456')  # 登录
    query.into_order_query()  # 进入订单管理
    query.order_query('2021101466060')  # 查询订单
    print(query.get_order_information())  # 获取订单信息
